import {
  Controller,
  Get,
  Post,
  HttpStatus,
  Res,
  Body,
  HttpException,
  UseInterceptors,
  UploadedFile
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { TestService } from './test.service';
import { Response } from 'express';

interface IPostBody {
  username: string;
  age: number;
  gender: string;
}

interface IInfoBody {
  username: string;
  gender: string;
}

@Controller('test')
export class TestController {
  constructor(private readonly testService: TestService) {}
  @Get('list')
  getList(@Res({ passthrough: true }) res: Response) {
    res.status(HttpStatus.OK);
    return { code: 0, data: this.testService.getList(), message: 'success' };
  }

  @Post('create')
  createUser(@Body() body: IPostBody, @Res({ passthrough: true }) res: Response) {
    console.log(body);
    if (body.username) {
      res.status(HttpStatus.OK);
      return { code: 0, data: `create user ${body.username} success`, message: 'success' };
    } else {
      throw new HttpException({ status: HttpStatus.BAD_REQUEST, error: '用户信息不完整' }, HttpStatus.BAD_REQUEST);
    }
  }

  @Post('info')
  @UseInterceptors(FileInterceptor('file'))
  createInfo(
    @UploadedFile() file: Express.Multer.File,
    @Body() body: IInfoBody,
    @Res({ passthrough: true }) res: Response
  ) {
    console.log(file);
    console.log(body);
    if (body) {
      res.status(HttpStatus.OK);
      return { code: 0, data: '', message: 'success' };
    } else {
      throw new HttpException({ status: HttpStatus.BAD_REQUEST, error: '用户信息不完整' }, HttpStatus.BAD_REQUEST);
    }
  }
}
